<template>
  <div class="wordNature">
    <wd-row class="wordMenu">
      <wd-input type="text" v-model="searchText" @confirm="onSearch" />
      <wd-row style="margin-top:20rpx;" class="wm-btns">
        <wd-button class="wm-btn" @click="onCreate" size="small">C</wd-button>
        <wd-button class="wm-btn" @click="saveToJSON" size="small">S</wd-button>
      </wd-row>
    </wd-row>
    <div class="wordList">
      <div class="wl-item">
        <div class="wl-word">
          <wd-input type="text" v-model="word.word" placeholder="word" />
        </div>
        <div class="wl-word-mean wlWordMean wwm">
          <div class="wwm-list">
            <div class="wwm-item" v-for="(mean, mi) in word.means" :key="mi">
              <div class="wwm-hd">
                <div class="wwm-type"></div>
                <div class="wwm-text">
                  <wd-textarea v-model="mean.text" auto-height placeholder="mean" />
                </div>
              </div>
              <div class="wwd-instances wwdInstance win">
                <div class="win-list">
                  <div class="win-item" v-for="(instance, ii) in mean.instances" :key="ii">
                    <div class="win-en">
                      <wd-textarea v-model="instance.en" auto-height placeholder="en" />
                    </div>
                    <div class="win-zh">
                      <wd-textarea v-model="instance.zh" auto-height placeholder="zh" />
                    </div>
                  </div>
                </div>
                <div class="win-option">
                  <wd-button class="win-create" @click="addWordMeanInstances(mean)" size="small" style="margin-top:10rpx;">
                    Instance
                  </wd-button>
                </div>
              </div>
            </div>
          </div>
          <div class="wwm-option">
            <wd-button class="wwm-create" @click="addWordMeans(word)" size="small" style="margin-top:10rpx;">Mean</wd-button>
          </div>
        </div>
        <div class="wl-note">
          <wd-textarea v-model="word.note" auto-height placeholder="note" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchText: '',
      nature: {
        list: []
      },
      word: {}
    }
  },
  methods: {
    saveToJSON() {
      uni.setStorageSync('nature', this.nature)
    },
    loadFromJSON() {
      this.nature = uni.getStorageSync('nature') || {
        list: []
      }
    },
    onSearch(e) {
      let word = this.nature.list.find((res) => res.word === e.value)
      if (word) {
        this.word = word
      }
    },
    searchWord() {},
    onCreate() {
      this.nature.list.push({
        word: '', //单词
        note: '', //笔记
        means: []
      })
    },
    addWordMeans(word) {
      word.means.push({
        wordType: '', //单词类型
        text: '', //解释
        note: '', //笔记
        instances: []
      })
    },
    addWordMeanInstances(mean) {
      mean.instances.push({
        en: '', //英文
        zh: '', //中文
        note: '' //笔记
      })
    }
  },
  mounted() {
    this.loadFromJSON()
  }
}
</script>
